﻿using System;
using System.Web.Security;
using TravelAgency.DAL;
using Telerik.Web.UI;

namespace TravelAgency.AspNet.Agents
{
    public partial class SearchTrips : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipUser currentUser = Membership.GetUser();
            this.Session["userId"] = currentUser.ProviderUserKey;
        }

        protected void RadListViewTrips_ItemCommand(object sender, Telerik.Web.UI.RadListViewCommandEventArgs e)
        {
            if (e.CommandName == RadListView.SelectCommandName)
            {
                RadListViewDataItem item = e.ListViewItem as RadListViewDataItem;
                string tripIdString = item.GetDataKeyValue("TripId").ToString();
                string peopleCountString = item.GetDataKeyValue("PeopleCount").ToString();
                int tripId = int.Parse(tripIdString);

                TravelAgencyDAO dao = new TravelAgencyDAO();
                using (dao)
                {
                    Trip selectedTrip = dao.GetTripByTripId(tripId);
                    this.FillTripData(selectedTrip);
                }

                this.LabelPeopleCount.Text = peopleCountString;
            }
        }

        private void FillTripData(Trip selectedTrip)
        {
            if (selectedTrip != null)
            {
                this.PanelTrip.Visible = true;
                this.LabelTripNotAvailable.Visible = false;
                this.PanelTripDetails.Visible = true;
                this.LabelTitle.Text = Server.HtmlEncode(selectedTrip.Title);
                this.LabelDescription.Text = Server.HtmlEncode(selectedTrip.Description);
                this.LabelStartDate.Text = string.Format("{0:yyyy.MMM.dd}", selectedTrip.StartDate);
                this.LabelEndDate.Text = string.Format("{0:yyyy.MMM.dd}", selectedTrip.EndDate);
                this.LabelPrice.Text = string.Format("{0:c}", selectedTrip.Price);
            }
        }

        protected string TitleWordWrapEval(string PropertyName)
        {
            string fullTitle = Eval(PropertyName).ToString();
            if (fullTitle.Length > 19)
            {
                string shortTitle = fullTitle.Substring(0, 16);
                return shortTitle + "...";
            }
            else
            {
                return fullTitle;
            }
        }

        protected void LinkButtonSearch_Click(object sender, EventArgs e)
        {
            string searchPhrase = this.TextBoxSearch.Text;
            this.RadListViewReservations.FilterExpressions.Clear();
            this.RadListViewReservations.FilterExpressions.BuildExpression().
                Contains("Trip.Title", searchPhrase).Build();
            this.RadListViewReservations.Rebind();

            this.PanelTrip.Visible = false;
        }

        protected void LinkButtonCancelReservation_Click(object sender, EventArgs e)
        {
            int tripId = (int)this.RadListViewReservations.SelectedItems[0].GetDataKeyValue("TripId");

            Trip trip;
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                trip = dao.GetTripByTripId(tripId);
            }

            if (trip.StartDate < DateTime.Now)
            {
                
                this.Response.Redirect("~/Users/CancelReservationNotAllowed.aspx");
            }
            else
            {
                int reservationId =
                    (int)this.RadListViewReservations.SelectedItems[0].GetDataKeyValue("ReservationId");
                this.Response.Redirect("~/Users/CancelReservation.aspx?reservationId=" + reservationId.ToString());
            }
        }        
    }
}